package com.college.collegeinfomanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college.collegeinfomanage.dto.FacultyDTO;
import com.college.collegeinfomanage.entity.Faculty;
import com.college.collegeinfomanage.mapper.FacultyMapper;
import com.college.collegeinfomanage.mapper.UniversityMapper;
import com.college.collegeinfomanage.service.FacultyService;
import com.college.collegeinfomanage.vo.FacultyListVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FacultyServiceImpl extends ServiceImpl<FacultyMapper, Faculty> implements FacultyService {

    @Resource
    private FacultyMapper facultyMapper;

    @Resource
    private UniversityMapper universityMapper;

    @Override
    public IPage<FacultyListVO> getPage(FacultyDTO facultyDTO) {
        LambdaQueryWrapper<Faculty> queryWrapper = new LambdaQueryWrapper<>();
        if (facultyDTO.getUniversityId() != null) {
            queryWrapper.eq(Faculty::getUniversityId, facultyDTO.getUniversityId());
        }
        if (facultyDTO.getFacultyType() != null && !"".equals(facultyDTO.getFacultyType())) {
            queryWrapper.like(Faculty::getFacultyType, facultyDTO.getFacultyType());
        }
        IPage<Faculty> page = new Page<>(facultyDTO.getPage(), facultyDTO.getSize());
        IPage<Faculty> facultyIPage = facultyMapper.selectPage(page, queryWrapper);
        List<Faculty> records = facultyIPage.getRecords();
        List<FacultyListVO> collect = records.stream()
                .map(faculty -> {
                    FacultyListVO facultyListVO = new FacultyListVO();
                    facultyListVO.setId(faculty.getId());
                    facultyListVO.setUniversityName(universityMapper.selectById(faculty.getUniversityId()).getName());
                    facultyListVO.setUniversityId(faculty.getUniversityId());
                    facultyListVO.setFacultyType(faculty.getFacultyType());
                    facultyListVO.setFacultyCount(faculty.getFacultyCount());
                    return facultyListVO;
                }).collect(Collectors.toList());
        IPage<FacultyListVO> page1 = new Page<>(facultyDTO.getPage(), facultyDTO.getSize());
        page1.setTotal(facultyIPage.getTotal());
        page1.setPages(facultyIPage.getPages());
        page1.setCurrent(facultyIPage.getCurrent());
        page1.setSize(facultyIPage.getSize());
        page1.setRecords(collect);
        return page1;

    }
}
